/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

#ifndef __dylab_TypeParser_h__
#define __dylab_TypeParser_h__

//===========================================================================================
#include <boost/lexical_cast.hpp>

//===========================================================================================
#include <dylab/utils/exceptions/EInvalidFormat.h>

//===========================================================================================
namespace dylab {

    class DYLAB_EXPORT TypeParser
    {
    public:
        /**
        */
        template <typename T>
        static T parse(const String & _str)
                {
                    try 
                    { 
                        return boost::lexical_cast<T>(_str); 
                    }
                    catch (boost::bad_lexical_cast) 
                    {
                        throw EInvalidFormat(String::fmt("Conversion of string '%2%' to type '%1%' has failed.", 
							typeid(T).name(), _str));
                    }
                }

        /**
        */
        template <typename T>
        static T parse(const String & _str, const T & _defaultValue)
                {
                    try
                    {
                        // return the translated value
                        return boost::lexical_cast<T>(_str);
                    }
                    catch (boost::bad_lexical_cast)
                    {
                        // conversion faild, so return the _defaultValue
                        return _defaultValue;
                    }
                }

        // String parsing specialisation
        template<>
        static String parse(const String & _str)
                { return _str; }
        template<>
        static String parse(const String & _str, const String &)
                { return _str; }

        // bool parsing specialisation
        template<>
        static bool parse(const String & _str)
                { 
                    if (_str == "on" || _str == "1" || _str == "yes")
                        return true;
                    else if (_str == "off" || _str == "0" || _str == "no")
                        return false;
					else throw EInvalidFormat(String::fmt("Can't convert string '%1%' to bool (should be 'on' or 'off').", _str));
                }
        template<>
        static bool parse(const String & _str, const bool & _defaultValue)
                { 
                    try { return parse<bool>(_str); }
                    catch (EInvalidFormat & ) { return _defaultValue; }
                }
    };
}
//===========================================================================================
#endif // __dylab_TypeParser_h__
